
 var app = getApp();

 var tools = require('../tools/tools.js');

Page({
  data:{
      
  },
  getMovieDatas:function(url){
    var self = this;
     wx.request({
       url: url,
       method:"GET",
       header:{
         "Content-Type":'JSON'
       },
       success:function(res){
          console.log(res);
        if(res.statusCode=="200"){
          var data = res.data.subjects,
              dataLen = data.length;
               switch(dataLen){
                 case 10 :
                  self.handleMovie(data,"正在热映",true);
                  break;
                 case 20 :
                  self.handleMovie(data,"其它电影",true);
                  break;
                 case 250:
                  self.handleMovie(data,"TOP250",true);
                  break;

               }
              
        }
       },
       fail:function(res){
        console.log(res);
      }
     })
  },
  
  handleMovie:function(data,info){
    var dataLen = data.length,
       IN_TAHEATERSOBJ={},
       COMING_SONGOBJ={},
       TOP250OBJ={};



    if(dataLen === 10){
     IN_TAHEATERSOBJ = tools.setSpliceMovieData(data,info,0,3); 
      this.setData({
        IN_TAHEATERSOBJ
      })
    
   
    }
    if(dataLen===20){
     COMING_SONGOBJ = tools.setSpliceMovieData(data,info,3,6);
     this.setData({
      COMING_SONGOBJ 
    })
    }
    if(dataLen===250){
     TOP250OBJ = tools.setSpliceMovieData(data,info,6,9);
     this.setData({
      TOP250OBJ
    })
    }
      
   },
   onMovieGrid:function(e){
       var info =e.currentTarget.dataset.info;
         wx.navigateTo({
           url: '../movie_grid/movie_grid?info='+info
         })
      
   },
   onToMovieDetail:function(e){
     var id = e.currentTarget.dataset.id;
         wx.navigateTo({
           url: `/movieDetail/movie_detail?id=${id}`
         })

   },
  onLoad:function(){
    this.getMovieDatas(app.IN_TAHEATERSURL);
    this.getMovieDatas(app.COMING_SONGURL);
    this.getMovieDatas(app.TOP250URL);
  }
})